<?php
namespace app\member\controller;
class Group extends Common{
    function __construct(){
        parent::__construct();
        $this->assign('_nav',json_encode(config("navs.1.children"),JSON_UNESCAPED_UNICODE));
    }
    // 分组设置
    function index($type=0){
    	$member_id=$this->user['id'];
    	$withName='';
        if($type>0){
            $withName='social';
    	}else{
    		$withName='device';
        }
        // 获取所有的成员
		$members=model($withName)->readAll(['member_id'=>$member_id],0,['field'=>'id,name']);
		$this->assign('members',$members->toJson());
    	$options=[
    		'withCount'=>$withName,
    	];
    	$list=model($this->table)->readAll(['member_id'=>$member_id,'type'=>$type],true,$options);
    	$this->assign('list',$list);
    	$this->assign('type',$type);

    	return view();
    }
    // 添加分组
    function add($id=0){
    	if(request()->isAjax()||request()->isPost()){
	    	$data=input('');
	    	if(empty($data)){
	    		$this->error('数据不完整');
	    	}
	    	$validate=$this->validate($data,[
	    		'name|分组名称'=>'require',
	    	]);
	    	if(true!==$validate){
	    		$this->error($validate);
	    	}
	    	
            $operation=[];
            if(isset($data['device_count'])){
                unset($data['device_count']);
            }
            if($id>0){
	    	    $operation=model($this->table)->edit($data);
	    	}else{
	    	    $data['member_id']=$this->user['id'];
	    	    $data['s_time']=time();
	    	    $operation=model($this->table)->add($data);
	    	}
	    	if(empty($operation)){
		    	$this->error(lang('Failed operation'));
	    	}
	    	$this->success(lang('Successful operation'));
    	}
    }
    // 移除、添加成员
    function append($diff=[],$group_id=0,$type=0){
    	if(request()->isAjax()||request()->isPost){
    		if(empty($diff)||$group_id<=0){
    			$this->error('数据不完整');
    		}
    		$has=model($this->table)->read(['id'=>$group_id,'member_id'=>$this->user['id']],'id');
    		if(!count($has)){
    			$this->error('不存在该分组');
    		}
    		$table=$type>0?'group_socail':'group_device';
    		$isRemove=model($table)->where(['group_id'=>$group_id,'relation_id'=>$diff[0]])->count();
    		if($isRemove){
                $where=[
                    'group_id'=>$group_id,
                    'relation_id'=>['relation_id','in',$diff]
                ];
    			$del=model($table)->del($where);
    			if($del){
    				$this->success('移除成功');
    				die;
    			}
    			$this->error('移除失败');
    		}else{
                $data=[];
                foreach ($diff as $v) {
                    $data[]=['group_id'=>$group_id,'relation_id'=>$v];
                }
    			$add=model($table)->add($data);
    			if(count($add)){
    				$this->success('添加成功');
    				die;
    			}
    			$this->error('添加失败');
    			
    		}
    	}
    }
    // 获取成员ID列表
    function getMemberIds($group_id=0,$type=0){
    	if(request()->isAjax()||request()->isPost){
	    	if($group_id<=0){
	    		$this->error('数据不存在');
	    	}
	    	$table=$type>0?'group_socail':'group_device';
	    	$list=model($table)->readAll(['group_id'=>$group_id],0,['field'=>'relation_id']);
	    	$ids=[];
	    	if(count($list)){
	    		foreach ($list as $v) {
	    			$ids[]=$v['relation_id'];
	    		}
	    	}
	    	$this->success('数据获取成功','',$ids);
    	}
    }
}